<?php

/**
 * Hoa Framework
 *
 *
 * @license
 *
 * GNU General Public License
 *
 * This file is part of HOA Open Accessibility.
 * Copyright (c) 2007, 2008 Ivan ENDERLIN. All rights reserved.
 *
 * HOA Open Accessibility is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HOA Open Accessibility is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HOA Open Accessibility; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @category    Framework
 * @package     Hoa_Convertor
 *
 */

/**
 * Hoa_Framework
 */
require_once 'Framework.php';

/**
 * Hoathis_Convertor_Format_Abstract
 */
importModule('Convertor.Format.Abstract');

/**
 * Hoa_Stream_Context
 */
import('Stream.Context');

/**
 * Hoa_File_Read
 */
import('File.Read.~');

/**
 * Class Hoathis_Convertor_Format_Currency
 *
 * Extending the Hoathis_Convertor_Format_Abstract class.
 *
 * @author      Gregory DARCHE <tetardo@gmail.com>
 * @copyright   Copyright (c) 2009 Gregory DARCHE.
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @version     0.1
 * @package     Hoathis_Convertor
 * @subpackage  Hoathis_Convertor_Format
 */
Class Hoathis_Convertor_Format_Currency extends Hoathis_Convertor_Format_Abstract {

    protected $units = array(
        'AED'	=> null,
        'ANG' 	=> null,
        'ARS' 	=> null,
        'AUD' 	=> null,
        'BGN' 	=> null,
        'BHD'	=> null,
        'BND' 	=> null,
        'BOB' 	=> null,
        'BRL' 	=> null,
        'BWP' 	=> null,
        'CAD' 	=> null,
        'CHF' 	=> null,
        'CLP' 	=> null,
        'CNY' 	=> null,
        'COP' 	=> null,
        'CZK' 	=> null,
        'DKK' 	=> null,
        'EEK' 	=> null,
        'EGP' 	=> null,
        'EUR' 	=> null,
        'FJD' 	=> null,
        'GBP' 	=> null,
        'HKD' 	=> null,
        'HNL' 	=> null,
        'HRK' 	=> null,
        'HUF' 	=> null,
        'IDR' 	=> null,
        'ILS' 	=> null,
        'INR' 	=> null,
        'ISK' 	=> null,
        'JPY' 	=> null,
        'KRW' 	=> null,
        'KWD' 	=> null,
        'KZT' 	=> null,
        'LKR' 	=> null,
        'LTL' 	=> null,
        'MAD' 	=> null,
        'MUR' 	=> null,
        'MXN' 	=> null,
        'MYR' 	=> null,
        'NOK' 	=> null,
        'NPR' 	=> null,
        'NZD' 	=> null,
        'OMR' 	=> null,
        'PEN' 	=> null,
        'PHP' 	=> null,
        'PKR' 	=> null,
        'PLN' 	=> null,
        'QAR' 	=> null,
        'RON' 	=> null,
        'RSD' 	=> null,
        'RUB' 	=> null,
        'SAR' 	=> null,
        'SEK' 	=> null,
        'SGD' 	=> null,
        'SIT' 	=> null,
        'SKK' 	=> null,
        'THB' 	=> null,
        'TRY' 	=> null,
        'TTD' 	=> null,
        'TWD' 	=> null,
        'UAH' 	=> null,
        'USD' 	=> null,
        'VEB' 	=> null,
        'ZAR' 	=> null
    );

    protected $filters = array(
        'Pourcent',
        'Round',
        'Number',
        'Unit'
    );

    Const GOOGLE_URL = "http://www.google.com/ig/calculator?hl=en&q=%d%s=?%s";

    protected function convert( $value, $from, $to ) {

        Hoa_Stream_Context::getInstance('google', 'http')->addOptions(array(
            'timeout' => 30
        ));

        try {

            $content = new Hoa_File_Read(
                sprintf(self::GOOGLE_URL, $value, $from, $to),
                Hoa_File::MODE_READ,
                'google'
            );
        }
        catch ( Hoa_File_Exception $e ) {

            throw new Hoa_Convertor_Exception(
                $e->getFormattedMessage(),
                $e->getCode()
            );
        }

        $result = preg_replace("/([a-zA-Z0-9_]+?):/" , "\"$1\":", $content->readAll());
        $result = json_decode($result);

        $tmp = explode(' ', $result->rhs);
        return $tmp[0];
    }
}